package net.ryian.sms.service.impl;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * 江苏美圣短信网关实现
 * 
 * @author wangcheng
 *
 */
public class MsSmsService extends BaseSmsService {

	protected String URL = "http://112.74.76.186:8030/service/httpService/httpInterface.do?method=sendUtf8Msg";
	private String username;
	private String veryCode;
	private String password;

	public MsSmsService() {
		super();
		username = prop.getProperty("mssms.username");
		veryCode = prop.getProperty("mssms.verycode");
		password = prop.getProperty("mssms.password");
	}

	@Override
	/**
	 * 发送短信
	 * @param mobile 手机号码，多个手机号码用","分隔
	 * @param msg 发送的消息
	 * @return 是否发送成功
	 */
	public void send(String mobile, String msg) throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(URL);
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("username", username),
				new NameValuePair("veryCode", veryCode),
				new NameValuePair("mobile", mobile),
				new NameValuePair("password", password),
				new NameValuePair("content", msg),
				new NameValuePair("msgtype", "1") };
		post.setRequestBody(data);
		client.executeMethod(post);
		String result = new String(post.getResponseBodyAsString().getBytes(
				"UTF-8"));
		post.releaseConnection();
		
		String result_code = null;
		if (result != null && result.contains("<status>")&&result.contains("</status>")) {
			result_code = result.substring(result.indexOf("<status>")+8, result.indexOf("</status>"));
		}
		if (!result_code.startsWith("0")) {
			throw getSendExeption(result);
		}
	}

	/**
	 * 根据模板发送短信
	 * 
	 * @param mobile
	 *            手机号码，多个手机号码用","分隔
	 * @param template
	 *            发送的模板名称，对应sms.properties中的模板key值
	 * @param values
	 *            模板中的值，按顺序提供
	 */
	@Override
	public void send(String mobile, String template, String... values)
			throws Exception {
		StringBuffer buf = new StringBuffer();
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				if (buf.length() != 0) {
					buf.append(",");
				}
				buf.append("@").append(i + 1).append("@").append("=")
						.append(values[i]);
			}
		}
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(URL);
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("username", username),
				new NameValuePair("password", password),
				new NameValuePair("veryCode", veryCode),
				new NameValuePair("mobile", mobile),
				new NameValuePair("content", buf.toString()),
				new NameValuePair("msgtype", "2"),
				new NameValuePair("tempid", prop.getProperty(template)) };
		post.setRequestBody(data);

		client.executeMethod(post);
		String result = new String(post.getResponseBodyAsString().getBytes(
				"UTF-8")).trim();
		post.releaseConnection();
		String result_code = null;
		if (result != null && result.contains("<status>")&&result.contains("</status>")) {
			result_code = result.substring(result.indexOf("<status>")+8, result.indexOf("</status>"));
		}
		if (!result_code.startsWith("0")) {
			throw getSendExeption(result_code);
		}
	}

	private Exception getSendExeption(String result) {
		String ex = null;
		if ("100".equals(result)) {
			ex = "发送失败";
		} else if ("101".equals(result)) {
			ex = "用户账号不存在或密码错误";
		} else if ("102".equals(result)) {
			ex = "账号已禁用";
		} else if ("103".equals(result)) {
			ex = "参数不正确";
		} else if ("105".equals(result)) {
			ex = "短信内容超过500字、或为空、或内容编码格式不正确";
		} else if ("106".equals(result)) {
			ex = "手机号码超过100个或合法的手机号码为空，每次最多提交100个号";
		} else if ("108".equals(result)) {
			ex = "余额不足";
		} else if ("109".equals(result)) {
			ex = "指定访问ip地址错误";
		} else if (result.startsWith("110")) {
			ex = "短信内容存在系统保留关键词:" + result.substring(4);
		} else if ("114".equals(result)) {
			ex = "模板短信序号不存在";
		} else if ("115".equals(result)) {
			ex = "短信签名标签序号不存在";
		} else {
			ex = "未知错误，result:" + result;
		}
		return new Exception(ex);
	}

	@Override
	public void send(String mobile, String templatecode, String signature, String... values) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer buf = new StringBuffer();
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				if (buf.length() != 0) {
					buf.append(",");
				}
				buf.append("@").append(i + 1).append("@").append("=")
						.append(values[i]);
			}
		}
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(URL);
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("username", username),
				new NameValuePair("password", password),
				new NameValuePair("veryCode", veryCode),
				new NameValuePair("mobile", mobile),
				new NameValuePair("content", buf.toString()),
				new NameValuePair("msgtype", "2"),
				new NameValuePair("tempid", templatecode) };
		post.setRequestBody(data);

		client.executeMethod(post);
		String result = new String(post.getResponseBodyAsString().getBytes(
				"UTF-8")).trim();
		post.releaseConnection();
		String result_code = null;
		if (result != null && result.contains("<status>")&&result.contains("</status>")) {
			result_code = result.substring(result.indexOf("<status>")+8, result.indexOf("</status>"));
		}
		if (!result_code.startsWith("0")) {
			throw getSendExeption(result_code);
		}
	}

}
